package com.sdc.servlets;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class Arquivos
 */
public class ArquivoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ArquivoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		if (ServletFileUpload.isMultipartContent(request)) {
			ServletFileUpload servletFileUpload = new ServletFileUpload(
					new DiskFileItemFactory());
			List fileItemsList = null;
			try {
				fileItemsList = servletFileUpload.parseRequest(request);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			String optionalFileName = "";
			FileItem fileItem = null;

			Iterator it = fileItemsList.iterator();
			while (it.hasNext()) {
				FileItem fileItemTemp = (FileItem) it.next();
				if (fileItemTemp.isFormField()) {

					if (fileItemTemp.getFieldName().equals("filename"))
						optionalFileName = fileItemTemp.getString();
				} else
					fileItem = fileItemTemp;
			}

			if (fileItem != null) {
				String fileName = fileItem.getName();
			}
		}

	}

}
